<template>
	<view class="auto">
		<view class="auto-container">
			<view class="auto-title">
				<view>欢迎使用校园智能管理系统</view>
				<text>授权微信头像、昵称</text>
			</view>

			<view class="auto-logo flex-center">
				<image src="/static/images/logo-left.png" class="auto-logo-left" mode=""></image>
				<image src="/static/images/logo.png" mode="" class="logo"></image>
				<image src="/static/images/logo-left.png" mode="" class="auto-logo-right"></image>
			</view>
			<view class="auto-tis">
				<view>为提供优质服务，需要获取你的以下信息：</view>
				<text>你的公开信息(头像、昵称等）</text>
			</view>
			<!-- <view class="auto-submit" @click="navigate()">
				
			</view> -->
			<button class="auto-submit" type="default" open-type="getUserInfo" @click="getUserInfo()">授权进入</button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		set,
		get
	} from '../../config/db.js'
	export default {
		data() {
			return {

			}
		},
		onLoad(option) {
			if(option.parentId){
				set('parentId',option.parentId)//测试使用 广西大学
			}
			if(option.code){
				this.getLogin(option.code)
			}
			let token = get('authorization')
			let info = get('info')
			if (token) {  
				if (info.type == '01' || info.type == '03') {
					uni.redirectTo({
						url: '../userSide/index'
					})
				}
				if (info.type == '02' || info.type == '04') {
					uni.redirectTo({
						url: '../approvalForm/index'
					})
				}
				if (info.type == '05') {
					uni.redirectTo({
						url: '../schoolGate/index',

					})
				}
			}
			this.showPhoto()
		},

		methods: {
			showPhoto(){
				this.$api.showPhoto({}).then(res=>{
					this.$db.set('showPhoto',res.data.flag)
				}).catch(res=>{})
			},
			// 截取url中的code方法
			getUrlCode() {
				var url = location.href; //获取打开的公众号的路径
				let winUrl = url;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(url.indexOf('?') + 1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						var items = strs[i].split('=');
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				}
				return theRequest;
			},
			
			getLogin(code){
				let _this = this
				let data = {
					universityId:get('parentId') || 110,
					js_code: code
				}
				//发起网络请求
				_this.$api.verifyCode({
					data: data
				}).then(res2 => {
					set('info', res2.data)
					set('authorization', res2.data.authorization)
				
					if (res2.data.type == '01' || res2.data.type == '03') {
						uni.redirectTo({
							url: '../userSide/index'
						})
					}
					if (res2.data.type == '02' || res2.data.type == '04') {
						uni.redirectTo({
							url: '../approvalForm/index'
						})
					}
					if (res2.data.type == '05') {
						uni.redirectTo({
							url: '../schoolGate/index'
						})
					}
				}).catch(res2 => {
					if(res2.data.data){
						set('authorization', res2.data.data.authorization)
					}
					// //10:新人员，11：您的资料正在审查，请耐心等待！  12:您的资料审批不通过，具体请联系您所选学院的负责人！,0人员审查通过
					if (res2.data.code == '10' || res2.data.code == '12') {
						uni.navigateTo({
							url: '../register/index'
						})
						return
					}
				
					_this.$uTips(res2.data.message)
				})
			},
			
			getUserInfo() {
				let _this = this
				// uni.navigateTo({
				// 	url:'../register/index'
				// })
				let appid = 'wxe2ffb711c5d98394'
				var local = encodeURIComponent('https://xmcr.nngyun.cn/pages/authorization/index')
				var wxUrl =
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
					appid +
					'&redirect_uri=' +
					local +
					'&response_type=code' +
					'&scope=snsapi_userinfo' +
					'&state=STATE#wechat_redirect';
					window.location.href = wxUrl;
				// 无用
				return
				// 获取用户信息
				uni.getUserInfo({
					// 获取信息成功
					success(res) {
						// 成功后进行登录,获取code
						uni.login({
							success(res1) {
								if (res1.code) {
									let data = {
										universityId:get('parentId') || 110,
										js_code: res1.code
									}
									//发起网络请求
									_this.$api.verifyCode({
										data: data
									}).then(res2 => {
										set('info', res2.data)
										set('authorization', res2.data.authorization)

										if (res2.data.type == '01' || res2.data.type == '03') {
											uni.redirectTo({
												url: '../userSide/index'
											})
										}
										if (res2.data.type == '02' || res2.data.type == '04') {
											uni.redirectTo({
												url: '../approvalForm/index'
											})
										}
										if (res2.data.type == '05') {
											uni.redirectTo({
												url: '../schoolGate/index'
											})
										}
									}).catch(res2 => {
										if(res2.data.data){
											set('authorization', res2.data.data.authorization)
										}
										// //10:新人员，11：您的资料正在审查，请耐心等待！  12:您的资料审批不通过，具体请联系您所选学院的负责人！,0人员审查通过
										if (res2.data.code == '10' || res2.data.code == '12') {
											uni.navigateTo({
												url: '../register/index'
											})
											return
										}

										_this.$uTips(res2.data.message)
									})

								} else {
									console.log('登录失败！' + res.errMsg)
								}
							}
						})
					},
					fail(e) {
						_this.$uTips(e.errMsg)
					}
				})


			}
		},
	}
</script>

<style lang="scss">
	.auto-container {
		padding: 0 26rpx;
		overflow: hidden;
	}

	.auto-title {
		margin-top: 114rpx;

		view {
			font-size: 48rpx;
			margin-bottom: 17rpx;
		}

		text {
			color: #7F7F7F;
			font-size: 32rpx;
		}
	}

	.auto-logo {
		height: 464rpx;
		position: relative;

		.logo {
			width: 188rpx;
			height: 238rpx;
			margin-top: 144rpx;
		}

		.auto-logo-left {
			position: absolute;
			left: -200rpx;
			top: 40rpx;
			width: 434rpx;
			height: 492rpx;
		}

		.auto-logo-right {
			position: absolute;
			right: -50rpx;
			top: -130rpx;
			width: 154rpx;
			height: 258rpx;
		}
	}

	.auto-tis {
		view {
			color: #3B3B3B;
			font-size: 28rpx;
			margin-bottom: 28rpx;
		}

		text {
			color: #7F7F7F;
			font-size: 26rpx;
		}
	}

	.auto-submit {
		color: #fff !important;
		// padding: 30rpx 0;
		font-size: 32rpx;
		margin-top: 50rpx;
		text-align: center;
		border-radius: 46rpx;
		background-color: #4D7CFB !important;
	}
</style>
